package day8.homework;
//   A线程睡眠10毫秒，对变量加1，B线程睡眠20毫秒对变量加1，持续3秒
//   查看多次运行的结果是否一致
public class ChangeVar extends Thread{
    static int num;
    public void run() {
        long start = System.currentTimeMillis();
        String name = Thread.currentThread().getName();

        while (true){
            long end = System.currentTimeMillis();
            if (end-start >= 3000) break;
            if (name.equals("A")){
                try {
                    sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                num++;
            }else{
                try {
                    sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                num++;
            }
        }
        System.out.println(name+":"+num);
    }
    public static void main(String[] args) {
        Thread t1 = new ChangeVar();
        Thread t2 = new ChangeVar();
        t1.setName("A");
        t2.setName("B");
        t1.start();
        t2.start();
    }
}
